<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>学生列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <!-- 搜索栏 -->
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="student-search-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="studentName" autocomplete="off" class="layui-input" placeholder="请输入姓名">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">学院</label>
                            <div class="layui-input-inline">
                                <input type="text" name="institute" autocomplete="off" class="layui-input" placeholder="请输入学院">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">专业</label>
                            <div class="layui-input-inline">
                                <input type="text" name="major" autocomplete="off" class="layui-input" placeholder="请输入专业">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">年级</label>
                            <div class="layui-input-inline">
                                <input type="text" name="grade" autocomplete="off" class="layui-input" placeholder="请输入年级（如2021级）">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">班级</label>
                            <div class="layui-input-inline">
                                <input type="text" name="clazz" autocomplete="off" class="layui-input" placeholder="请输入班级（如计科1班）">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">联系方式</label>
                            <div class="layui-input-inline">
                                <input type="text" name="tel" autocomplete="off" class="layui-input" placeholder="请输入手机号">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="data-search-btn">
                                <i class="layui-icon">&#xe615;</i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <!-- 学生列表表格 -->
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <!-- 操作栏模板 -->
        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
        </script>

    </div>
</div>

<!-- 引入layui依赖 -->
<script src="../../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    layui.use(['form', 'table', 'layer', 'jquery'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        // 初始化变量
        var searchData = {
            studentName: '',
            institute: '',
            major: '',
            grade: '',
            clazz: '',
            tel: ''
        };
        var currentPage = 1;
        var pageSize = 15;

        // 构建URL
        function buildUrl(page, size, params) {
            function encode(val) {
                return val && val.trim() !== '' ? encodeURIComponent(val.trim()) : '_';
            }
            return '/students/' + page + '/' + size + '/' +
                encode(params.studentName) + '/' +
                encode(params.grade) + '/' +
                encode(params.tel) + '/' +
                encode(params.institute) + '/' +
                encode(params.major) + '/' +
                encode(params.clazz);
        }

        // 初始化表格
        var tableIns = table.render({
            elem: '#currentTableId',
            url: buildUrl(currentPage, pageSize, searchData),
            method: 'GET',
            response: {
                statusCode: 200
            },
            parseData: function(res) {
                return {
                    code: res.code,
                    msg: res.msg,
                    count: res.data.total,
                    data: res.data.records
                };
            },
            cols: [[
                {type: 'checkbox', width: 50, align: 'center'},
                {field: 'studentId', title: '学生ID', width: 100, align: 'center', sort: true},
                {field: 'studentName', title: '姓名', width: 120, align: 'center'},
                {field: 'grade', title: '年级', width: 100, align: 'center'},
                {field: 'major', title: '专业', width: 150, align: 'center'},
                {field: 'clazz', title: '班级', width: 120, align: 'center'},
                {field: 'institute', title: '学院', width: 180, align: 'center'},
                {field: 'tel', title: '电话号码', width: 150, align: 'center'},
                {field: 'email', title: '电子邮件', width: 200, align: 'center'},
                {field: 'sex', title: '性别', width: 80, align: 'center', templet: function(d){
                        return d.sex;
                    }},
                {field: 'cardId', title: '身份证号', width: 200, align: 'center'},
                {title: '操作', width: 130, align: 'center', toolbar: '#currentTableBar'}
            ]],
            page: {
                curr: currentPage,
                limit: pageSize,
                limits: [10, 15, 20, 50],
                layout: ['prev', 'page', 'next', 'limit', 'count'],
                groups: 5
            },
            skin: 'line',
            even: true
        });

        // 监听搜索按钮
        form.on('submit(data-search-btn)', function (data) {
            searchData = {
                studentName: data.field.studentName || '',
                institute: data.field.institute || '',
                major: data.field.major || '',
                grade: data.field.grade || '',
                clazz: data.field.clazz || '',
                tel: data.field.tel || ''
            };
            currentPage = 1;
            tableIns.reload({
                url: buildUrl(currentPage, pageSize, searchData),
                page: { curr: 1 }
            });
            return false;
        });

        // 监听分页切换
        table.on('page(currentTableFilter)', function(obj) {
            currentPage = obj.curr;
            pageSize = obj.limit;
            tableIns.reload({
                url: buildUrl(currentPage, pageSize, searchData),
                page: { curr: currentPage, limit: pageSize }
            });
        });

        // 监听操作栏事件
        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                // 存储学生数据到localStorage
                localStorage.setItem('currentStudent', JSON.stringify(data));

                layer.open({
                    title: '编辑学生信息',
                    type: 2,
                    shade: 0.3,
                    area: ['700px', '600px'],
                    content: 'studentEdit.html',
                    success: function(layero, index) {
                        // 弹窗加载完成后的回调
                    }
                });
            } else if (obj.event === 'delete') {
                layer.confirm('确认删除该学生？', {
                    icon: 3,
                    title: '提示'
                }, function(index) {
                    // 发送DELETE请求
                    axios.delete('/student/' + data.studentId)
                        .then(function(response) {
                            if (response.data.code === 200) {
                                layer.msg('删除成功');
                                tableIns.reload();
                            } else {
                                layer.msg('删除失败: ' + response.data.msg);
                            }
                        })
                        .catch(function(error) {
                            layer.msg('请求失败: ' + error.message);
                        });
                    layer.close(index);
                });
            }
        });
    });
</script>
</body>
</html>